﻿using GN.Pay.TradeChannel.Configure;
using GN.Pay.TradeChannel.Gyb.Request;
using GN.Pay.TradeChannel.Gyb.Response;
using GN.Pay.TradeChannel.Transfers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb
{
    /// <summary>
    /// 贵阳银行基本服务
    /// </summary>
    [Serializable]
    public abstract class GybServiceBase : ATradeChannel<IDictionary<string, string>>, ITransfers
    {
        protected readonly GybConfigure channelConfigure;

        protected GybServiceBase()
        {
            this.channelConfigure = new GybConfigure(this);
        }
        public override ITradeChannelConfigure ChannelConfigure
        {
            get
            {
                return this.channelConfigure;
            }
        }

        /// <summary>
        /// 获取提供者全称
        /// </summary>
        public sealed override string ChannelProvider
        {
            get
            {
                return "贵阳银行股份有限公司";
            }
        }

        public override Type TradeChannelConfigureInfoType
        {
            get
            {
                return typeof(GybConfigureInfo);
            }
        }

        private static IChannel TempChannel = TradeChannelInfo.Create("Gyb", "贵阳银行");

        /// <summary>
        /// 获取付款的订单号
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string GetPayOrderNo(IDictionary<string, string> message)
        {
            if (message == null || message.Count == 0)
            {
                return null;
            }
            PayNotifyResponse res = new PayNotifyResponse(message);
            if (!res.IsSuccess())
            {
                return null;
            }
            var entity = res.ToEntity();
            return entity.TermSsn;
        }

        /// <summary>
        /// 获取退款批次号
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string GetRefundNo(IDictionary<string, string> message)
        {
            return null;
        }

        /// <summary>
        /// 订单付款通知响应
        /// </summary>
        /// <param name="message">消息</param>
        public PayNotifyResponseEntity OrderPayNotifyResponse(IDictionary<string, string> message)
        {
            if (message == null || message.Count == 0)
            {
                throw this.ThrowChannelValidationException("贵阳银行通知错误:请求体为空白");
            }
            PayNotifyResponse res = new PayNotifyResponse(message);
            if (!res.IsSuccess())
            {
                if (!string.IsNullOrWhiteSpace(res.ErrorCode) && !string.IsNullOrWhiteSpace(res.ErrorMsg))
                {
                    throw this.ThrowChannelValidationException("贵阳银行通知错误->" + res.ErrorCode + ":" + res.ErrorMsg);
                }
                if (!string.IsNullOrWhiteSpace(res.ErrorCode))
                {
                    throw this.ThrowChannelValidationException("贵阳银行通知错误代码:" + res.ErrorCode);
                }
                if (!string.IsNullOrWhiteSpace(res.ErrorMsg))
                {
                    throw this.ThrowChannelValidationException("贵阳银行通知错误消息:" + res.ErrorMsg);
                }
                throw this.ThrowChannelValidationException("贵阳银行通知错误:未知，错误代码与消息均为null或空白值。");
            }
            if (!res.SignVeryfy(this, this.channelConfigure))
            {
                this.ThrowChannelValidationException("贵阳银行通知验签失败");
            }
            var entity = res.ToEntity();
            if (!entity.IsSuccess())
            {
                if (entity.RespCode == null)
                {
                    entity.RespCode = "";
                }
                this.ThrowChannelValidationException("贵阳银行验签成功，但响应码错误:" + entity.RespCode);
            }
            return entity;
        }

        /// <summary>
        /// 付款通知
        /// </summary>
        /// <param name="sercice">服务</param>
        /// <param name="message">消息</param>
        public override void OrderPayNotify(INotifyService sercice, IDictionary<string, string> message)
        {
            var entity = OrderPayNotifyResponse(message);
            var result = entity.ToPayResult(this);
            if (result != null)
            {
                sercice.OrderPayNotify(result);
            }
        }

        public override void OrderRefundNotify(INotifyService sercice, IDictionary<string, string> message)
        {
            throw new NotImplementedException();
        }

        public override ITradeOrderQueryResult OrderSingleQuery(DateTime queryDate, string orderNo, string tradeNo)
        {
            if (string.IsNullOrWhiteSpace(orderNo))
            {
                TradeChannelExceptionUtils.ThrowClientRequestException(this.ChannelId, "查询订单时，订单号不能为空");
            }
            var req = new QueryRequest(this, queryDate, channelConfigure, orderNo);
            var res = req.Build();
            if (!res.IsSuccess())
            {
                return null;
            }
            if (!res.SignVeryfy())
            {
                TradeChannelExceptionUtils.ThrowClientRequestException(this.ChannelId, "验证未通过");
            }
            return res.ToResult();
        }

        //不可靠
        public override bool OrderSingleQueryIsReliable
        {
            get
            {
                return false;
            }
        }

        public override ITradeRefundApplyResult RefundApply(IRefundApplyInfo apply)
        {
            if (apply == null)
            {
                this.ThrowClientRequestValidationException(nameof(apply) + "请求退款参数为空！");
            }
            RefundRequest req = new RefundRequest(this, apply, this.channelConfigure);
            var response = req.Build(this, this.channelConfigure, apply);
            return response.ToResult();
        }

        public sealed override string PayNotifyResponse(bool isSuccess)
        {
            if (isSuccess)
            {
                return "success";
            }
            return "fail";
        }

        public sealed override string RefundNotifyResponse(bool isSuccess)
        {
            if (isSuccess)
            {
                return "success";
            }
            return "fail";
        }

        /// <summary>
        /// 转账验证
        /// </summary>
        /// <param name="apply"></param>
        public void TransfersApplyValidate(ITransfersApplyInfo apply)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 转账申请
        /// </summary>
        /// <param name="apply"></param>
        /// <returns></returns>
        public ITransfersApplyResult TransfersApply(ITransfersApplyInfo apply)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 转账通知
        /// </summary>
        /// <param name="sercice"></param>
        /// <param name="message"></param>
        public void TransfersNotify(ITransfersNotifyService sercice, object message)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 转账响应
        /// </summary>
        /// <param name="isSuccess"></param>
        /// <returns></returns>
        public string TransfersNotifyResponse(bool isSuccess)
        {
            throw new NotImplementedException();
        }
    }
}
